package cn.com.victorysoft.ylgl.service.fracturingpressure.controller;

import cn.com.victorysoft.ylgl.service.commons.mybatis.paging.bean.DataPaging;
import cn.com.victorysoft.ylgl.service.commons.web.JsonMessage;
import cn.com.victorysoft.ylgl.service.commons.web.RequestContext;
import cn.com.victorysoft.ylgl.service.commons.web.User;
import cn.com.victorysoft.ylgl.service.fracturingpressure.domain.PressureConstruction;
import cn.com.victorysoft.ylgl.service.fracturingpressure.domain.PressureConstructionVO;
import cn.com.victorysoft.ylgl.service.fracturingpressure.service.fracturing.IPressureConstructionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 主压施工参数
 */
@Controller
@RequestMapping({"fracturingPressure/pressureConstruction"})
public class PressureConstructionController {
    private static final Logger logger = LoggerFactory.getLogger(PressureConstructionController.class);

    @Resource
    private IPressureConstructionService service;

    public PressureConstructionController() {
    }

    @RequestMapping(
            value = {"/list.wet"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    public String list() {
        return "fracturingPressure/pressureConstruction/list";
    }


    /**
     * 列表
     * @return
     */
    @RequestMapping(
            value = {"/editDetail.wet"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    public String editDetail() {
        return "fracturingPressure/pressureConstruction/detail";
    }

    @RequestMapping(
            value = {"/get.wds"},
            method = {RequestMethod.POST,RequestMethod.GET}
    )
    @ResponseBody
    public JsonMessage get(@RequestParam(value="id", required=true) String id) {
        PressureConstruction entity = this.service.getById(id);
        return (new JsonMessage()).success(entity);
    }

    @RequestMapping(
            value = {"/add.wds"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public JsonMessage createEntity(@ModelAttribute("model") PressureConstructionVO entity) {
        User user = RequestContext.getCurrentUser();
        this.service.insert(entity);
        PressureConstructionVO newEntity = service.getDtoBy(entity.getDataId());
        return (new JsonMessage()).success(newEntity);
    }


    @RequestMapping(
            value = {"/update.wds"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public JsonMessage updateEntity(@ModelAttribute("model") PressureConstructionVO entity) {
        //先根据faid查以前方案的信息
        PressureConstruction oldEntity = service.getById(entity.getDataId());
        //用户需要历史数据

        this.service.updateByPrimaryKey(entity);
        PressureConstructionVO newEntity = service.getDtoBy(entity.getDataId());
        return (new JsonMessage()).success(newEntity);
    }


    /**
     * @param ids:1;2;3;
     * @return
     */
    @RequestMapping(value={"/delete.wds"}, method={RequestMethod.POST}, produces={"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonMessage delete(@RequestParam(value="ids", required=true) String ids)
    {
        try
        {
            this.service.deleteByKeys(ids);
            return new JsonMessage().success();
        }
        catch (Exception e)
        {
            logger.error("删除出错: {}", e);
        }
        return new JsonMessage().failure("失败");
    }

    @RequestMapping(
            value = {"/queryPagingList.wds"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public JsonMessage queryPagingList(@RequestParam(value = "page",required = false) String page,
                                       @RequestParam(value = "rows",required = false) String rows,
                                       @RequestParam(value = "yt",required = false) String yt,
                                       @RequestParam(value = "qk",required = false) String qk,
                                       @RequestParam(value = "dk",required = false) String dk,
                                       @RequestParam(value = "cw",required = false) String cw,
                                       @RequestParam(value = "jh",required = false) String jh) {

        DataPaging<PressureConstructionVO> dataPaging =  this.service.queryPagingList(yt,qk,dk,cw,jh,rows,page);
        return (new JsonMessage()).success(dataPaging);
    }

}